package xyz.bali16.module.sensitiveword.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import xyz.bali16.application.controller.BaseController;
import xyz.bali16.application.core.enums.ApiEnum;
import xyz.bali16.application.core.model.Result;
import xyz.bali16.module.sensitiveword.entity.SensitiveWord;
import xyz.bali16.module.sensitiveword.model.SensitiveWordAddParam;
import xyz.bali16.module.sensitiveword.model.SensitiveWordPageParam;
import xyz.bali16.module.sensitiveword.model.SensitiveWordUpdateParam;
import xyz.bali16.module.sensitiveword.service.SensitiveWordService;


import javax.validation.Valid;

import static xyz.bali16.application.core.model.Result.*;


/**
 * @author bali2 on 9/3/2023.
 * @version 1.0
 */
@RestController
@RequestMapping(BaseController.API+"sensitiveWord")
@Api(tags = "敏感词接口")
@AllArgsConstructor

public class SensitiveWordController extends BaseController{
    @Autowired
    SensitiveWordService sensitiveWordService;

    @GetMapping
    @ApiOperation("分页条件查询敏感词")
    //@PreAuthorize("@auth.permission('plugin:sensitiveword:query')")
    public Result<IPage<SensitiveWord>> pageQuery(@Valid SensitiveWordPageParam param) {
        IPage<SensitiveWord> tagIPage = sensitiveWordService.getPage(param);
        return success(ApiEnum.GET_SUCCESS, tagIPage);
    }

    @PostMapping
    @ApiOperation("添加敏感词")
    //@PreAuthorize("@auth.permission('plugin:sensitiveword:add')")
    public Result<String> add(@RequestBody @Valid SensitiveWordAddParam param) {
        try {
            sensitiveWordService.addSensitiveWord(param);
            return response(ApiEnum.ADD_SUCCESS);
        } catch (Exception e) {
            return failure(ApiEnum.ADD_ERROR, "新增敏感词失败", e);
        }
    }

    @PutMapping
    @ApiOperation("修改敏感词")
    //@PreAuthorize("@auth.permission('plugin:sensitiveword:update')")
    public Result<String> update(@RequestBody @Valid SensitiveWordUpdateParam param) {
        try {
            sensitiveWordService.updateSensitiveWord(param);
            return response(ApiEnum.UPDATE_SUCCESS);
        } catch (Exception e) {
            return failure(ApiEnum.UPDATE_ERROR, "更新敏感词失败", e);
        }
    }

    @DeleteMapping("/{sensitiveWordId}")
    @ApiOperation("删除敏感词")
    @ApiImplicitParam(name = "sensitiveWordId", value = "敏感词id", paramType = "path", required = true)
    //@PreAuthorize("@auth.permission('plugin:sensitiveword:delete')")
    public Result<String> deleteSensitiveWord(@PathVariable("sensitiveWordId") String sensitiveWordId) {
        try {
            sensitiveWordService.delete(sensitiveWordId);
            return response(ApiEnum.DELETE_SUCCESS);
        } catch (Exception e) {
            return failure(ApiEnum.DELETE_ERROR, e);
        }
    }

}
